package cn.ztelovecly.springboot.framework.util.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *
 */
public class FileUtil {
    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(String filePath) throws IOException {
        return getBytes(new File(filePath));
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(File file) throws IOException {
        byte[] buffer;
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
        return buffer;
    }

    /**
     * 删除文件
     * @param filePath  文件的路径
     * @return          删除的结果
     * @throws SecurityException
     */
    public static boolean deleteFile(String filePath) throws SecurityException{
        File file = new File(filePath);
        return file.delete();
    }
}
